import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { BannerList, CategoryList, GoodsList, HotGoodsList } from '@/types'
import { http } from '@/utils/request'

export const useHomeStore = defineStore(
  'home',
  () => {
    // 获取所有分类导航
    const categoryList = ref<CategoryList>([])
    const getAllCateGory = async () => {
      const res = await http<CategoryList>('GET', '/home/category/head')
      categoryList.value = res.data.result
    }
    // 获取轮播图数据
    const bannerList = ref<BannerList>([])
    const getBannerList = async () => {
      const res = await http<BannerList>('GET', '/home/banner')
      bannerList.value = res.data.result
    }
    // 获取新鲜好物
    const newGoodsList = ref<GoodsList>([])
    const getNewGoodsList = async () => {
      const res = await http<GoodsList>('GET', '/home/new', { limit: 4 })
      newGoodsList.value = res.data.result
    }
    // 获取人气推荐
    const hotGoodsList = ref<HotGoodsList>([])
    const getHotGoodsList = async () => {
      const res = await http<HotGoodsList>('GET', '/home/hot')
      hotGoodsList.value = res.data.result
    }

    return {
      categoryList,
      getAllCateGory,
      bannerList,
      getBannerList,
      newGoodsList,
      getNewGoodsList,
      hotGoodsList,
      getHotGoodsList
    }
  },
  {
    persist: {
      key: 'rabbit-mall-home',
      paths: ['categoryList']
    }
  }
)
